﻿using RpcClient;

using RpcHelper;

using RpcManageClient;

using RpcModel.Model;

using Wedonek.RpcStore.Service.DAL;
using Wedonek.RpcStore.Service.Interface;
using Wedonek.RpcStore.Service.Model;

namespace Wedonek.RpcStore.Service.Collect
{
        internal class ServerClientLimitCollect : BasicCollect<ServerClientLimitDAL>, IServerClientLimitCollect
        {
                private IRpcServerCollect _RpcServer => this.GetCollect<IRpcServerCollect> ();
                public void Sync (ServerClientLimitData add)
                {
                        long limitId = this.BasicDAL.FindLimitId (add.RpcMerId, add.ServerId);
                        if (limitId == 0)
                        {
                                this.BasicDAL.Add (add);
                        }
                        else
                        {
                                this.BasicDAL.Set (limitId, add.ConvertMap<ServerClientLimitData, ServerClientLimit> ());
                        }
                        this._RpcServer.RefreshClientLimit (add.RpcMerId, add.ServerId);
                }


                public void DropConfig (long rpcMerId, long serverId)
                {
                        long limitId = this.BasicDAL.FindLimitId (rpcMerId, serverId);
                        if (limitId == 0)
                        {
                                return;
                        }
                        ServerClientLimitData config = this.BasicDAL.Drop (limitId);
                        this._RpcServer.RefreshClientLimit (config.RpcMerId, config.ServerId);
                }
                public ServerClientLimitData GetConfig (long rpcMerId, long serverId)
                {
                        ServerClientLimitData limit = this.BasicDAL.Get (rpcMerId, serverId);
                        if (limit == null)
                        {
                                throw new ErrorException ("rpc.limit.not.find");
                        }
                        return limit;
                }

        }
}
